<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">扫描市民卡</block>
		</cu-custom>
		<form class="form">
			<view class="cu-form-group">
				<view class="title">卡号</view>
				<input placeholder="可以输入卡号" type="text" :value="cardId" @confirm="getCardInfo(this.cardId)" confirm-type="done"></input>
				<button class='cu-btn bg-green shadow' @click="openQrcode">扫码</button>
			</view>
		</form>

		<view class="cu-list menu-avatar">
			<view class="cu-item">
				<view class="cu-avatar round lg" :style="'background-image:url('+userInfo.head_photo+');'"></view>
				<view class="content">
					<view class="text-grey">{{userInfo.real_name}}</view>
					<view class="text-gray text-sm flex">
						<view class="text-cut">
							<text class="cuIcon-vipcard text-red  margin-right-xs"></text>
							{{userInfo.IDcard}}
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<form>
			<view class="cu-form-group margin-top">
				<view class="title color12">店铺 :</view>
				<input :value="shopInfo.shop_name" name="input" disabled class="color12"></input>
			</view>
			<view class="cu-form-group">
				<view class="title color12">地址 :</view>
				<input :value="shopInfo.shop_address" name="input" disabled class="color12"></input>
			</view>
			<view class="cu-form-group">
				<view class="title color12">电话 :</view>
				<input :value="shopInfo.shop_iphone" name="input" disabled class="color12"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">原价 :</view>
				<input v-model="shopInfo.original_price" placeholder='输入总价格' type="digit"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">优惠后金额 :</view>
				<input v-model="shopInfo.price" placeholder='输入优惠后的金额' type="digit"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">商品说明 :</view>
				<input v-model="shopInfo.store_title" placeholder='输入商品名称' name="input"></input>
			</view>
			
		</form>

		<button class="cu-btn block bg-blue margin-tb-sm lg" @click="saveForm()">
				<text class="cuIconfont-spin"></text> 保存</button>
	</view>
</template>

<script>
	import {
		useCardGetUserInfo,
		getShopInfoById,
		InsertShopRecord
	} from '../../../../api/business.js'
	export default {
		data() {
			return {
				loading: 'cuIcon-loading2',
				userInfo: {
					real_name: '...',
					IDcard: '...'
				},
				shopInfo: {
					id:'',
					shop_address:'',
					shop_iphone: '',
					shop_name: '',
					store_id: '',
					price:'',
					original_price:'',
					store_title:''
				},
				cardId:''
			}
		},
		onShow: function() {
			// this.getCardInfo(445327470904)
			this.getShopInfo()
		},
		methods: {
			openQrcode() {
				uni.scanCode({
					onlyFromCamera: true,
					success: (result) => {
						console.log(result, '扫码信息1')
						let data = result.result
						this.cardId = data
						this.getCardInfo(data)
					}
				})
			},
			getCardInfo(code) {
				useCardGetUserInfo(code).then(ret => {
					if (ret.data.errcode == 0) {
						let userInfo = ret.data.data
						console.log(userInfo, '返回的用户信息')
						userInfo.IDcard = this.$common.cardidReply(userInfo.IDcard)
						this.userInfo = userInfo
					} else {
						this.userInfo = {}
						this.$common.toast(ret.data.errmsg)
					}
				})
			},
			getShopInfo() {
				getShopInfoById().then(ret => {
					let result = ret.data
					if (result.errcode == 0) {
						this.shopInfo.id = result.data.id
						this.shopInfo.shop_address = result.data.shop_address
						this.shopInfo.shop_iphone = result.data.shop_iphone
						this.shopInfo.shop_name = result.data.shop_name
					} else {
						// TODO
					}
				})
			},
			saveForm(){
				let data = {
					original_price:this.shopInfo.original_price,
					price:this.shopInfo.price,
					use_type:'2',
					store_title:this.shopInfo.store_title,
					uid:this.userInfo.user_id,
					is_test:'1'
				}
				if(data.uid == '' || data.uid == undefined){
					this.$common.showModal('警告','请先扫码!!')
					return false
				}
				if(data.original_price == '' || data.original_price == undefined){
					this.$common.showModal('警告','请输入原始金额!!')
					return false
				}
				if(data.price == '' || data.price == undefined){
					this.$common.showModal('警告','输入优惠后的金额!!')
					return false
				}
				if(data.store_title == '' || data.store_title == undefined){
					this.$common.showModal('警告','请输入商品信息!!')
					return false
				}
				console.log(data, '传递的数据')
				InsertShopRecord(data).then(ret=>{
					let rest = ret.data
					if(rest.errcode == 0){
						this.$common.toast('保存成功')
						this.shopInfo.price = ''
						this.shopInfo.original_price = ''
						this.shopInfo.store_title = ''
					}else{
						this.$common.toast(rest.errmsg)
					}
				})
			}
		},
	}
</script>

<style scoped>
	.form {
		padding: 10px;
	}
	.color12{
		color: #8799A3;
	}
</style>
